package legumes.client.ui;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.VerticalAlign;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.user.client.DOM;

public class LoadingUIManager {

	private static LoadingUIManager s_instance;
	private Element loadingElt;
	private Element loadingMsg;
	private Element loadingPct;
	private boolean hasBeenInitialized = false;
	private boolean isVisible = false;
	
	public static LoadingUIManager getInstance(){
		if(s_instance==null){
			s_instance = new LoadingUIManager();
		}
		return s_instance;
	}
	
	private LoadingUIManager(){
	}
	
	public void init(){
		loadingElt = DOM.getElementById("loadingDiv");
		loadingElt.getStyle().setVerticalAlign(VerticalAlign.MIDDLE);
		loadingElt.getStyle().setDisplay(Display.BLOCK);
		
		loadingMsg = DOM.getElementById("loadingMsg");
		updateLoadingMessage("Chargement en cours... Merci de patienter.");
		
		loadingPct = DOM.getElementById("loadingPct");
		updateLoadingPct(0);
		hasBeenInitialized=true;
		show();
	}
	
	public void show(){
		isVisible=true;
		loadingElt.getStyle().setVisibility(Visibility.VISIBLE);
	}
	
	public void hide(){
		isVisible=false;
		loadingElt.getStyle().setVisibility(Visibility.HIDDEN);
	}
	
	public void updateLoadingMessage(String msg_){
		if(hasBeenInitialized && isVisible)
			loadingMsg.setInnerText(msg_);
	}
	
	public void updateLoadingPct(int pct_){
		if(hasBeenInitialized && isVisible)
			loadingPct.setInnerText(Integer.toString(pct_) + " %");
	}
}
